package com.xs.tool.common.weather.wsdl;

import com.xs.tool.common.weather.WsdlClient;
import org.apache.cxf.endpoint.Client;

import java.util.List;

/**
 * 天气工具类
 * @author xs
 * @date 20200125
 */
public class WeatherUtil {
    private static WeatherUtil instance = null;
    public static WeatherUtil getInstance(){
        if (instance == null) {
            synchronized (WeatherUtil.class) {
                if (instance == null) {
                    instance = new WeatherUtil();
                }
            }
        }
        return instance;
    }

    private static final String WEATHER_WSDL1 = "https://xiaosun-pub.oss-cn-hangzhou.aliyuncs.com/jflyfox/bbs/image/20210125_101144_923119.wsdl";

    private WeatherWSSoap getWeatherWSSoap(){
        try {
            //=============================================================================
            //第一种方式获取视图
            WeatherWS weatherWS = new WeatherWS();
            //获取服务实现类
            WeatherWSSoap weatherWSSoap = weatherWS.getPort(WeatherWSSoap.class);
            //=============================================================================

            //=============================================================================
            //第二种方式获取视图
            /*URL url = null;
                try {
                    url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

                QName qName = new QName("http://WebXml.com.cn/", "WeatherWS");

                Service service = Service.create(url, qName);
                WeatherWSSoap weatherWSSoap = service.getPort(WeatherWSSoap.class);*/
            //=============================================================================
            return weatherWSSoap;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * getRegionDataset 方法
     *
     * 获得中国省份、直辖市、地区；国家名称（国外）和与之对应的ID
     * 输入参数：无，返回数据：DataSet
     * 此 DataSet("getRegion") 包含二个表：1. Tables("Province") 中国省份、直辖市、地区
     * 表； 2. Tables("Country") 外国国家表。
     * Item("RegionName") 类型 String，为省份、直辖市、地区或国外国家名称；
     * Item("RegionID") 类型 String 为对应的 ID。
     * @return
     */
    public String getRegionDataset(){
        try {
            WeatherWSSoap weatherWSSoap = getWeatherWSSoap();
            GetRegionDatasetResponse.GetRegionDatasetResult getRegionDatasetResult = weatherWSSoap.getRegionDataset();
            System.out.println(getRegionDatasetResult.getAny());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public String getCity(String city){
        try {
            WeatherWSSoap weatherWSSoap = getWeatherWSSoap();
            //调用服务器接口，发送数据并返回数据
            ArrayOfString arrayOfString = weatherWSSoap.getWeather(city, "");
            //控制台打印数据
            List<String> list = arrayOfString.getString();
            for(String str : list) {
                System.out.println(str);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

